<?php

namespace app\consts;


use app\model\site\Config as ConfigModel;
use Illuminate\Support\Arr;

class Config
{
    const LOGO = "site.logo";

    /*-----------------------------------------------------------------------------------------------------------------*/
    protected $items = [];
    /**
     * 获取单例
     * @return static
     */
    public static function getInstance()
    {
        $instance = new static();
        $items = ConfigModel::select(['value',"key","data_type"])->get()->toArray();
        foreach ($items as $item) {
            $instance->set($item["key"],\app\utils\Transform::to($item["value"],$item["data_type"]));
        }
        return $instance;
    }


    public function get($key, $default = null)
    {
        return Arr::get($this->items, $key, $default);
    }


    public function set($key, $value = null)
    {
        $keys = is_array($key) ? $key : [$key => $value];

        foreach ($keys as $key => $value) {
            Arr::set($this->items, $key, $value);
        }
    }

    public function all()
    {
        return $this->items;
    }

}